/**
 * Copyright (c) 2010 - 2012, Sina Inc. All rights reserved.
 * @fileoverview Sina  新建案例里产品图片---上传
 * @author bowen liu | liubowen082@163.com
 * @version 1.1 | 2013-4-13
 * @example
 * {uploadBox // 容器名}
 */


LJS.define('cpt.editAuthGroup',function(ljs){
	
	var editAuthGroup = {
		showAdd:function(op, opw){
			var that = this;
			if(this.lay){
				this.lay.show();
				return
				}
			var arr = [],opt = {
				 id: '',
				 name: '',
				 ids: opw.split(',')
				}
			$.extend(opt,op)
			$.each(JAJU.groups,function(i,a){
					var checked = '';
					$.each(opt.ids,function(j,b){
							if(a.id==b){
								checked = 'checked="checked"'
								return false
								}
							})
					arr.push('<div><span><input type="checkbox" node_type="checkBoxs" value="'+a.id+'" '+checked+'></span><font>'+a.name+'</font></div>')
				});
			
			this.lay = new ljs.mod.cPrompt({
				tpl:'<div class="infor inforNew">\
					<div class="title"><span class="txt">信息提示</span><span class="btnExit"><a href="javascript:void(0)" event_type="del"></a></span></div>\
					<div class="fillList">\
						<ul>\
							<li class="fillTxt">\
								<div class="tit">权限：</div>\
								<div class="inputCom clearfix">'
									+arr.join('')+
								'</div>\
							</li>\
						</ul>\
					</div>\
					<div class="btnEn"><a class="btn-et" event_type="ok" href="javascript:void(0)"><span>确定</span></a><a class="btn-ex" href="javascript:void(0)" event_type="del"><span>取消</span></a></div>\
				</div>'
			});
			this.lay.show();
			$('[event_type="ok"]',this.lay.win).on('click',function(e){
				/*xiaogang add start*/
//				$.each(arr,function(ii,aa){
//					alert(aa.getAttribute('value'));
//				})
				var arr = [];
				$.each($('[node_type="checkBoxs"]:checked'), function(ii, aa) {
					arr.push(aa.value);
				})
				var resource_ids = arr.join(','); // 以逗号分隔的被选中的id
				/*xiaogang add end*/
				that.post(op, resource_ids);
				return false;
			});
			$(this.lay).on('onDel',function(){
								that.lay = null;
								})
			},
		post:function(opt, resource_ids){
			$('[node_type="error"]',$(this.lay.win)).hide()
//			var data = this.check(),that = this;
//			if(!data){
//				$('[node_type="error"]',$(that.lay.win)).show().find('.colorRem').html('请完整填写用户信息')
//				return 
//				}
			var name = $.trim($('[node_type="groupName"]').val());
			var data = {id:opt, resource_ids:resource_ids,action:'edit_group'};
			$.ajax({
			  type: "post",
			  url: "edit_group",
			  dataType: "json",
			  data:data,
			  cache:false,
			  success:function(json){
				  if(json.status=='100000'){
					  window.location.reload();
				  }else{
					  alert('添加失败');
					  //$('[node_type="error"]',$(that.lay.win)).show().find('.colorRem').html(json.msg)
				}
			  }
			});
			},
		check:function(){
			if(!this.lay){
				return
				}
			var name = $.trim($('[node_type="groupName"]',$(this.lay.win)).val())//node_type="groupName"
			var id = $('[node_type="checkBoxs"]:checked',$(this.lay.win));
			var ids = []
			
			$.each(id,function(i,a){
							   ids.push($(a).val())
							   })
			if(name=='' || ids.length==0){
				return false
				}
			return {groupId:ids.join(),name:name}
			}
		}

    return editAuthGroup;
    
});

